package com.api.syh56.shengyunhui.service.impl;

import com.api.syh56.shengyunhui.entity.Car;
import com.api.syh56.shengyunhui.entity.DedicatedLines;
import com.api.syh56.shengyunhui.entity.Driver;
import com.api.syh56.shengyunhui.entity.User;
import com.api.syh56.shengyunhui.entity.dto.DriverDTO;
import com.api.syh56.shengyunhui.entity.vo.DriverVO;
import com.api.syh56.shengyunhui.exception.BaseException;
import com.api.syh56.shengyunhui.service.*;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Objects;

@Service
public class AdminDriverServiceImpl implements AdminDriverService {

    @Autowired
    private UserService userService;

    @Autowired
    private DriverService driverService;

    @Autowired
    private DedicatedLinesService dedicatedLinesService;

    @Override
    public Page<DriverVO> select(Integer pageNum, Integer pageSize) {
//        Page<Driver> page = new Page<>(pageNum, pageSize);
//        DriverVO driverVO = new DriverVO();
//        List<Driver> list = driverService.list();
//        for ()
//        Page<DriverVO> driverVOPage = new Page<>(pageNum, pageSize);
//        QueryWrapper<Driver> driverQueryWrapper = new QueryWrapper<>();
//        driverService.selectdetails();
//        return driverVOPage;
        return null;
    }

    @Override
    public void assign(Long dedID, Long driId) {
        QueryWrapper<Driver> driverQueryWrapper = new QueryWrapper<>();
        driverQueryWrapper.lambda().eq(Driver::getId,driId);
        Driver driver = driverService.getOne(driverQueryWrapper);
        if (Objects.isNull(driver)) {
            throw new BaseException("司机ID不存在");
        }
        Driver driverNew = new Driver();
        BeanUtils.copyProperties(driver,driverNew);
        driverNew.setDedId(dedID);
        driverService.update(driverNew,driverQueryWrapper);
    }


    /**
     * 给专线指派司机
     *
     *
     * 货物信息 开发票状态（显示） 总金额
     */
    @Override
    public void insert(DriverDTO driverDTO) {
        QueryWrapper<User> userQueryWrapper = new QueryWrapper<>();
        userQueryWrapper.lambda().eq(User::getUserName,driverDTO.getUserName());
        User userDB = userService.getOne(userQueryWrapper);
        if(Objects.nonNull(userDB)){
            throw new BaseException("已存在该用户名");
        }else {
            User user = new User();
            BeanUtils.copyProperties(driverDTO,user);
            userService.save(user);
            QueryWrapper<User> userW = new QueryWrapper<>();
            userW.lambda().eq(User::getUserName,user.getUserName());
            User getUser = userService.getOne(userW);
            Long uid = getUser.getId();
            QueryWrapper<Driver> driverQueryWrapper = new QueryWrapper<>();
            driverQueryWrapper.lambda().eq(Driver::getUid,uid);
            QueryWrapper<DedicatedLines> dedicatedLinesQueryWrapper = new QueryWrapper<>();
            dedicatedLinesQueryWrapper.lambda().eq(DedicatedLines::getUid,uid);

            Driver driver = driverService.getOne(driverQueryWrapper);
            DedicatedLines dedicatedLines = dedicatedLinesService.getOne(dedicatedLinesQueryWrapper);
            if (Objects.nonNull(driver)){
                throw new BaseException("已存在该用户名的司机");
            }else {
                Driver driverNew = new Driver();
                BeanUtils.copyProperties(driverDTO,driverNew);
                driver.setUid(uid);
                driver.setDedId(dedicatedLines.getId());
                driverService.save(driver);
            }
        }
    }
}
